﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using BusinessEntity;
using BusinessLogic;
using BusinessService;
using CentalineWeb.Common;


namespace CentalineWeb
{
    public partial class Dept : System.Web.UI.Page
    {
        private TreeNode currentNode = null;
        public string DeptListJson = string.Empty;
        public string DeptListJson2 = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            DeptListJson = new DeptManager().DeptListToLevelJsonCommon(new DeptManager().GetlAll(false));
            DeptListJson2 = new DeptManager().DeptListToLevelJsonCommon(new DeptManager().GetlAll(false));
            if (!IsPostBack)
            {
                bindtree();
                trvWorkSpace.Nodes[0].Selected = true;
                currentNode = trvWorkSpace.Nodes[0];
            }
        }
        #region 绑定部门结构
        private void bindtree()
        {
            var list = new DeptManager().GetlAll(false);
            foreach (var department in list)
            {
                if (string.IsNullOrEmpty(department.ParentId))
                {
                    var node = new TreeNode(department.FullName, department.DeptId);
                    trvWorkSpace.Nodes.Add(node);
                    node.Collapse();
                    addChildren(node, list);
                    break;
                }
            }
        }
        #endregion
        #region 递归添加子节点

        private void addChildren(TreeNode node, List<Department> list)
        {
            foreach (var department in list)
            {
                if (department.ParentId == node.Value)
                {
                    var n = new TreeNode(department.FullName, department.DeptId);
                    node.ChildNodes.Add(n);
                    node.Collapse();
                    addChildren(n, list);
                }

            }
        }
        #endregion

        DeptService ds = new DeptService();
        protected void trvWorkSpace_SelectedNodeChanged(object sender, EventArgs e)
        {
            var deptid = trvWorkSpace.SelectedValue;
            var empList = ds.GetEmpsByDeptId(deptid);
            gvUsers.DataSource = empList;
            gvUsers.DataBind();

            txtDeptName1.Text = trvWorkSpace.SelectedNode.Text;
            txtAddress1.Text = trvWorkSpace.SelectedNode.Parent.Value;
            txtAddress.Text = trvWorkSpace.SelectedNode.Value;
        }
        #region 搜索
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SearchDept(txtCondition.Text.Trim());
        }

        private void SearchDept(string deptname)
        {
            foreach (TreeNode rn in trvWorkSpace.Nodes)
            {
                var node = FindNode(rn, deptname);
                if (node != null)
                {
                    node.Selected = true;
                    trvWorkSpace_SelectedNodeChanged(null, null);
                    ExpandParent(node);
                    return;
                }
                else
                {
                    node = FindNodeByChars(rn, deptname.ToLower());
                    if (node != null)
                    {
                        node.Selected = true;
                        trvWorkSpace_SelectedNodeChanged(null, null);
                        ExpandParent(node);
                        return;
                    }
                }
            }
        }
        #endregion
        private void ExpandParent(TreeNode node)
        {
            if (node.Parent != null)
            {
                node.Parent.Expand();
                ExpandParent(node.Parent);
            }
        }

        private TreeNode FindNode(TreeNode tnParent, string strValue)
        {
            TreeNode treeNode2 = new TreeNode();
            if (tnParent == null) return null;
            if (tnParent.Text.IndexOf(strValue) != -1) return tnParent;
            if (tnParent.Depth > 0) tnParent.CollapseAll();
            TreeNode tnRet = null;
            foreach (TreeNode tn in tnParent.ChildNodes)
            {
                tnRet = FindNode(tn, strValue);
                if (tnRet != null) break;
            }
            return tnRet;
        }

        private TreeNode FindNodeByChars(TreeNode tnParent, string strValue)
        {
            TreeNode treeNode2 = new TreeNode();
            if (tnParent == null) return null;
            if (GetStringSpell.GetChineseSpell(tnParent.Text).IndexOf(strValue) != -1) return tnParent;
            if (tnParent.Depth > 0) tnParent.CollapseAll();
            TreeNode tnRet = null;
            foreach (TreeNode tn in tnParent.ChildNodes)
            {
                tnRet = FindNodeByChars(tn, strValue);
                if (tnRet != null) break;
            }
            return tnRet;
        }

        protected void gvUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void gvUsers_PageIndexChanging1(object sender, GridViewPageEventArgs e)
        {
            gvUsers.PageIndex = e.NewPageIndex;
            trvWorkSpace_SelectedNodeChanged(null, null);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var deptname = txtDeptName.Text.Trim();
            var pdeptId = txtAddress.Text.Trim();
            var deptId = "dept" + DateTime.Now.ToString("yyyyMMddHHmmssff");
            var dept = new Department()
            {
                DeptId = deptId,
                FullName = deptname,
                ParentId = pdeptId,
                Status = "营业"
            };
            new DeptService().AddOne(dept);
            trvWorkSpace.Nodes.Clear();
            bindtree();
            SearchDept(deptname);
        }

        protected void btnSave1_Click(object sender, EventArgs e)
        {
            var deptname = txtDeptName1.Text.Trim();
            var pdeptId = txtAddress1.Text.Trim();
            var selectedid = trvWorkSpace.SelectedValue;
            new DeptService().UpdateOne(selectedid, deptname, pdeptId);
            trvWorkSpace.Nodes.Clear();
            bindtree();
            SearchDept(deptname);
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            string t = string.Empty;
            var p = trvWorkSpace.SelectedNode.Parent;
            if (p != null)
            {
                    t = p.Text;
            }
            var selectedid = trvWorkSpace.SelectedValue;
            new DeptService().RevokeDept(selectedid);
            trvWorkSpace.Nodes.Clear();
            bindtree();
            SearchDept(t);
        }
        /// <summary>
        /// 同步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSync_click(object sender, EventArgs e)
        {
            var result = new DeptManager().SyncDepartments();
            trvWorkSpace.Nodes.Clear();
            bindtree();
            trvWorkSpace.Nodes[0].Selected = true;
            currentNode = trvWorkSpace.Nodes[0];
            this.ClientScript.RegisterStartupScript(typeof(string), "report", "alert('已同步" + result + "个部门信息')", true);
        }

    }
}